package com.example.zhangxin.zyfypt_124zx.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhangxin.zyfypt_124zx.R;
import com.example.zhangxin.zyfypt_124zx.bean.LoginBean;
import com.example.zhangxin.zyfypt_124zx.iface.LoginListener;
import com.example.zhangxin.zyfypt_124zx.model.LoginModel;

public class LoginActivity extends AppCompatActivity {

    private TextView tv,tv2,tv3;
    private EditText et,et2;
    private Button bt,bt2;

    private Switch sw;
    private String username="",password="",sessionID="";//用户名密码
    private SharedPreferences sp;


   // private LoginBean list=null;//保存数据

    LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean beanList) {

           System.out.println("----"+beanList.getSessionid());
            Toast.makeText(LoginActivity.this,"----"+beanList.getSessionid(), Toast.LENGTH_SHORT).show();
           username=beanList.getUsername();
           // password=beanList.getPassword();
            sessionID=beanList.getSessionid();
            if(sessionID!=null){
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                   saveSP();
                   Toast.makeText(LoginActivity.this, "11111", Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
            }

            //username.equals(sp.getString("username",null))&& password.equals(sp.getString("password",null))
//                if(username.equals(beanList.getName())&&password.equals(beanList.getPassword())) {
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//
//                    saveSP();
//                    Toast.makeText(LoginActivity.this, "11111", Toast.LENGTH_SHORT).show();
//
//                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
//                    startActivity(intent);
//                }

        }

        @Override
        public void onFail(String msg) {
           Toast.makeText(LoginActivity.this, "失败："+msg,Toast.LENGTH_SHORT).show();
            return ;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        tv = (TextView) findViewById(R.id.textView);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        et = (EditText) findViewById(R.id.editText);
        et2 = (EditText) findViewById(R.id.editText2);
        bt = (Button) findViewById(R.id.button);
        bt2= (Button) findViewById(R.id.button2);
        sw=(Switch)findViewById(R.id.switch1);

        //获取sharedperference对象
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //获取存储信息
        readSP();

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //登录按钮操作
                username=et.getText().toString();
                password=et2.getText().toString();

                LoginModel model=new LoginModel();
                model.getResultList(username,password,loginListener);
/*
                if(username.equals("123")&& password.equals("123")){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                    saveSP();
                    Toast.makeText(LoginActivity.this, "11111", Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                   // Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                   // startActivity(intent);
                }*/
            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    //保存sharedpreference
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.putString("sessionid",sessionID);
        editor.commit();//提交存储
    }

    //读取简单存储信息
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        String sess=sp.getString("sessionid",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            et.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }
}
